package com.songcha.library_network.api.factory

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.reflect.TypeToken
import okhttp3.RequestBody
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import java.lang.reflect.Type

class BaseConverterFactory(private val mGson:Gson) : Converter.Factory(){

    companion object{
        @JvmStatic
        fun create():BaseConverterFactory{
            return create(Gson())
        }

        @JvmStatic
        fun create(gson:Gson):BaseConverterFactory{
            return BaseConverterFactory(gson)
        }
    }

    override fun responseBodyConverter(
        type: Type,
        annotations: Array<out Annotation>,
        retrofit: Retrofit
    ): Converter<ResponseBody, *>? {
        val adapter:TypeAdapter<*> = mGson.getAdapter(TypeToken.get(type))
        return GsonResponseBodyConverter(mGson,adapter)
        //return super.responseBodyConverter(type, annotations, retrofit)
    }

    override fun requestBodyConverter(
        type: Type,
        parameterAnnotations: Array<out Annotation>,
        methodAnnotations: Array<out Annotation>,
        retrofit: Retrofit
    ): Converter<*, RequestBody>? {
        val adapter:TypeAdapter<*> = mGson.getAdapter(TypeToken.get(type))
        return GsonRequestBodyConverter(mGson,adapter)
        //return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit)
    }

}